package example.dawangba.com.exp4_z09315222;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

    private String[] names = {"白云山","九寨沟","故宫博物院","黄山"};
    private int[] headImagesSrc = {R.mipmap.p1,R.mipmap.p2,R.mipmap.p3,R.mipmap.p4};

    ListView listView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        listView = (ListView) findViewById(R.id.listView);
        listView.setAdapter(new MyAdapter());

        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = new Intent(MainActivity.this,ResultActivity.class);
                intent.putExtra("position",position);
                startActivity(intent);
            }
        });
    }

    class MyAdapter extends BaseAdapter{

        @Override
        public int getCount() {
            return names.length;
        }

        @Override
        public Object getItem(int position) {
            return names[position];
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View view = getLayoutInflater().inflate(R.layout.scenerylist_item,null);

            ImageView headImage = (ImageView)view.findViewById(R.id.image_head);
            headImage.setImageResource(headImagesSrc[position]);

            TextView sceName = (TextView) view.findViewById(R.id.scenery_name);
            sceName.setText(names[position]);
            return view;
        }
    }
}
